package com.ndphu.comic.gui.fragment.book;

import java.util.List;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ndphu.comic.R;
import com.ndphu.comic.gui.list.adapter.ChapterAdapter;
import com.ndphu.comic.gui.list.adapter.SlidingMenuAdatper;
import com.ndphu.comic.gui.list.model.SlidingMenuItem;
import com.ndphu.comic.model.Chapter;

public class OnlineBookChapterListFragment extends Fragment implements OnItemClickListener {

	private ListView mListChapter;
	private SlidingMenuAdatper mChapterAdapter;
	private List<Chapter> mChapters;
	private OnChapterSelectedListener mListener;
	private int mSelectedChapter = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_online_book_chapter_list, container, false);
		mListChapter = (ListView) view.findViewById(R.id.list);
		mListChapter.setBackgroundColor(getResources().getColor(android.R.color.white));
		mChapterAdapter = new ChapterAdapter(getActivity(), 0);
		mListChapter.setOnItemClickListener(this);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mListChapter.setAdapter(mChapterAdapter);
		for (Chapter chapter : mChapters) {
			mChapterAdapter.add(getChapterMenuItem(chapter));
		}
		mChapterAdapter.setSelectedIdx(mSelectedChapter);
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(100);
					getActivity().runOnUiThread(new Runnable() {

						@Override
						public void run() {
							mListChapter.smoothScrollToPosition(mSelectedChapter);
						}
					});
				} catch (InterruptedException e) {
				}
			}
		}).start();

	}

	private SlidingMenuItem getChapterMenuItem(Chapter chapter) {
		SlidingMenuItem result = new SlidingMenuItem();
		result.icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_listitem_chaper);
		result.title = "Chapter " + chapter.getChapterOrder();
		return result;
	}

	public void setChapterList(List<Chapter> chapters) {
		this.mChapters = chapters;
	}

	public static interface OnChapterSelectedListener {
		void onChapterSelected(Chapter chapter);
	}

	public void setOnChapterSelectedListener(OnChapterSelectedListener listener) {
		this.mListener = listener;
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
		if (this.mListener != null) {
			this.mListener.onChapterSelected(mChapters.get(position));
		}
		mChapterAdapter.setSelectedIdx(position);
	}

	public void setSelectedChapter(final int selectedIdx) {
		mSelectedChapter = selectedIdx;
		if (mChapterAdapter != null) {
			System.out.println(selectedIdx);
			mChapterAdapter.setSelectedIdx(selectedIdx);
			mChapterAdapter.notifyDataSetChanged();
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					getActivity().runOnUiThread(new Runnable() {

						@Override
						public void run() {
							mListChapter.smoothScrollToPosition(selectedIdx);
						}
					});
				}
			}).start();
		}
	}
}
